package controller

import (
	"github.com/cn-ygf/go-admin/internal/modular/system/model"
	"github.com/cn-ygf/go-admin/internal/modular/system/service"
	"github.com/cn-ygf/go-admin/pkg/log"
	"github.com/kataras/iris/v12"
)

var (
	systemLog = log.NamedLogger("system")
)

// getCurrentUser 获取当前登录用户
func getCurrentUser(ctx iris.Context) *model.SysUser {
	return ctx.Values().Get("user").(*model.SysUser)
}

// hasPermi 判断是否包含权限
// 如果无权限则自动返回内容
func hasPermi(ctx iris.Context, permi string) bool {
	user := getCurrentUser(ctx)
	if user == nil {
		responseServerError(ctx)
		return false
	}
	if !service.UserService.HasPermi(user.UserID, permi) {
		responseAccessDeny(ctx)
		return false
	}
	return true
}

func responseAccessDeny(ctx iris.Context) {
	r := map[string]any{
		"code": 403,
		"msg":  "未授权的操作",
	}
	ctx.StopWithJSON(200, r)
}

func responseServerError(ctx iris.Context) {
	r := map[string]any{
		"code": 500,
		"msg":  "服务器出现错误",
	}
	ctx.StopWithJSON(200, r)
}

func responseAdminError(ctx iris.Context) {
	r := map[string]any{
		"code": 601,
		"msg":  "不允许操作的数据",
	}
	ctx.StopWithJSON(200, r)
}
